'use strict';

const Controller = require('egg').Controller;

class AnalysisController extends Controller {
  // 衣服使用频率分析
  async clothingUsageFrequency() {
    const { ctx, service } = this;



    try {
      const { timeRange = '30d' } = ctx.query;
      
      const analysis = await service.analysis.getClothingUsageFrequency(
        ctx.state.user?.id || 1,
        timeRange
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: analysis,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 颜色使用偏好分析
  async colorPreferenceAnalysis() {
    const { ctx, service } = this;



    try {
      const analysis = await service.analysis.getColorPreferenceAnalysis(
        ctx.state.user?.id || 1
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: analysis,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 搭配风格分析
  async outfitStyleAnalysis() {
    const { ctx, service } = this;

    // 检查用户认证
    if (!ctx.state.user) {
      ctx.throw(401, '用户未认证');
    }

    try {
      const analysis = await service.analysis.getOutfitStyleAnalysis(
        ctx.state.user?.id || 1
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: analysis,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 购买建议分析
  async buyingSuggestionAnalysis() {
    const { ctx, service } = this;

    // 检查用户认证
    if (!ctx.state.user) {
      ctx.throw(401, '用户未认证');
    }

    try {
      const analysis = await service.analysis.getBuyingSuggestionAnalysis(
        ctx.state.user?.id || 1
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: analysis,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 季节性穿搭分析
  async seasonalWearAnalysis() {
    const { ctx, service } = this;

    // 检查用户认证
    if (!ctx.state.user) {
      ctx.throw(401, '用户未认证');
    }

    try {
      const { year = new Date().getFullYear() } = ctx.query;

      const analysis = await service.analysis.getSeasonalWearAnalysis(
        ctx.state.user?.id || 1,
        parseInt(year)
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: analysis,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 衣柜利用率分析
  async wardrobeUtilizationAnalysis() {
    const { ctx, service } = this;



    try {
      const analysis = await service.analysis.getWardrobeUtilizationAnalysis(
        ctx.state.user?.id || 1
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: analysis,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 支出分析
  async spendingAnalysis() {
    const { ctx, service } = this;



    try {
      const { timeRange = '12m' } = ctx.query;
      
      const analysis = await service.analysis.getSpendingAnalysis(
        ctx.state.user?.id || 1,
        timeRange
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: analysis,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 搭配评分趋势分析
  async outfitRatingTrendAnalysis() {
    const { ctx, service } = this;



    try {
      const { timeRange = '6m' } = ctx.query;
      
      const analysis = await service.analysis.getOutfitRatingTrendAnalysis(
        ctx.state.user?.id || 1,
        timeRange
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: analysis,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // 综合分析报告
  async comprehensiveReport() {
    const { ctx, service } = this;

    // 检查用户认证
    if (!ctx.state.user) {
      ctx.throw(401, '用户未认证');
    }

    try {
      const report = await service.analysis.getComprehensiveReport(
        ctx.state.user?.id || 1
      );

      ctx.body = {
        code: 200,
        message: '获取成功',
        data: report,
      };
    } catch (error) {
      ctx.throw(500, error.message);
    }
  }

  // AI简短prompt连通性测试接口
  async testShortPromptCall() {
    const { ctx, service } = this;
    try {
      // 直接调用aiAnalysis service的测试方法
      const result = await ctx.service.aiAnalysis.testShortPromptCall();
      ctx.body = {
        code: 200,
        message: 'AI简短prompt测试成功',
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: 'AI简短prompt测试失败',
        error: error.message,
      };
    }
  }
}

module.exports = AnalysisController;
